#! /usr/bin/python3
# coding=utf-8
# @time: 2025/10/30 16:28
# @Author:鹤川
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from airtest.core.api import *
from airtest.cli.parser import cli_setup

from common.doFile import myLogger


class BasePage():
    def __init__(self,poco=None):
        # if not cli_setup():
        # auto_setup(__file__, logdir=True, devices=["android://127.0.0.1:5037/emulator-5554?", ])

        if poco is None:
            self.poco:AndroidUiautomationPoco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
            self.start_app()
        else:
            self.poco:AndroidUiautomationPoco = poco

    def getText(self,locator):
        try:
            myLogger.info(f"获取文本成功，元素<{locator}>")
            return self.poco(locator).get_text()
        except Exception as e:
            print(f"获取文本失败，元素{locator}发生异常{e}")
            myLogger.error(f"获取文本失败，元素<{locator}>发生异常{e}")
            return ""


    def inputEle(self,locator,t):
        try:
            self.poco(locator).set_text(t)
        except Exception as e:
            print(f"输入失败，元素{locator}发生异常{e}")
            myLogger.error(f"输入失败，元素<{locator}>发生异常{e}")
        else:
            print(f"在{locator}中输入{t}成功")
            myLogger.info(f"在{locator}中输入《{t}》成功")

    def clickEle(self,locator):
        try:
            self.poco(locator).click()
        except Exception as e:
            print(f"点击失败，元素{locator}发生异常{e}")
            myLogger.error(f"点击失败，元素<{locator}>发生异常{e}")
        else:
            print(f"点击{locator}成功")
            myLogger.info(f"点击<{locator}>元素成功")

    # def assert_image_exists(self, template_path, record_pos, resolution, message=""):
    #     """通用图片断言方法"""
    #     try:
    #         template = Template(template_path, record_pos=record_pos, resolution=resolution)
    #         assert_exists(template, message)
    #         print(f"图片断言成功！{message}")
    #         return True
    #     except Exception as e:
    #         print(f"登录成功,图片断言失败！ {message}, 图片确实不存在: {e}")
    #         return False

    def start_app(self):
        start_app("com.kkpodcast")
        myLogger.info("启动app成功~~~")

    def stop_app(self):
        stop_app("com.kkpodcast")
        myLogger.info("停止app成功~~~~")






